﻿Type.registerNamespace('MS.InformationManagement.UserProfile');
MS.InformationManagement.UserProfile.NewProfileNameTimeout = null;
MS.InformationManagement.UserProfile.UpdateProfileNameTimeout = null;
MS.InformationManagement.UserProfile.Notification = null;

MS.InformationManagement.UserProfile.InitializeCreationForm = function (profileNameTextBox, submitButton, preloader,
    profileIDHiddenField, updateProfileNameTextBox, updateSubmitButton, updatePreloader) {
    MS.InformationManagement.Services.UserProfileService.set_path('/sites/docs/' + MS.InformationManagement.Services.UserProfileService.get_path());

    var textBox = $('#' + profileNameTextBox);
    var button = $('#' + submitButton);
    var spinner = $('#' + preloader);

    var profileIDHidden = $('#' + profileIDHiddenField);
    var updateTextBox = $('#' + updateProfileNameTextBox);
    var updateButton = $('#' + updateSubmitButton);
    var updateSpinner = $('#' + updatePreloader);

    textBox.bind("propertychange keyup input paste",
        function (event) {
            clearTimeout(MS.InformationManagement.UserProfile.NewProfileNameTimeout);
            MS.InformationManagement.UserProfile.NewProfileNameTimeout = setTimeout(
                function () {
                    MS.InformationManagement.UserProfile.ProfileExistsOnCreate(
                        textBox
                        , button
                        , spinner);
                }, 1000);
        }
    );

    updateTextBox.bind("propertychange keyup input paste",
        function (event) {
            clearTimeout(MS.InformationManagement.UserProfile.UpdateProfileNameTimeout);
            MS.InformationManagement.UserProfile.UpdateProfileNameTimeout = setTimeout(
                function () {
                    MS.InformationManagement.UserProfile.ProfileExistsOnUpdate(
                        profileIDHidden
                        , updateTextBox
                        , updateButton
                        , updateSpinner);
                }, 1000);
        }
    );

}

MS.InformationManagement.UserProfile.ProfileExistsOnCreate = function (profileNameTextBox, submitButton, preloader) {
    if (profileNameTextBox.val() != '') {
        preloader.addClass('inline');
        MS.InformationManagement.Services.UserProfileService.ProfileExists(
            profileNameTextBox.val()
            , MS.InformationManagement.UserProfile.OnAjaxComplete
            , MS.InformationManagement.UserProfile.OnAjaxException
            , { submitButton: submitButton, preloader: preloader }
        );
    }
}

MS.InformationManagement.UserProfile.ProfileExistsOnUpdate = function (profileIDHidden, profileNameTextBox, submitButton, preloader) {
    if (profileNameTextBox.val() != '') {
        preloader.addClass('inline');
        MS.InformationManagement.Services.UserProfileService.ProfileExistsOnUpdate(
            parseInt(profileIDHidden.val())
            , profileNameTextBox.val()
            , MS.InformationManagement.UserProfile.OnAjaxComplete
            , MS.InformationManagement.UserProfile.OnAjaxException
            , { submitButton: submitButton, preloader: preloader }
        );
    }
}

MS.InformationManagement.UserProfile.OnAjaxComplete = function (result, userState) {
    userState.preloader.removeClass('inline');
    if (MS.InformationManagement.UserProfile.Notification != null) { MS.InformationManagement.UserProfile.Notification.remove(); }
    if (result === true) {
        userState.submitButton.attr('disabled', 'disable');
        MS.InformationManagement.UserProfile.Notification = $('<span class="notification warning">Ya existe un perfil con este nombre.</span>');
    }
    else {
        userState.submitButton.removeAttr('disabled');
        MS.InformationManagement.UserProfile.Notification = $('<span class="notification ok">El nombre se encuentra disponible.</span>');
    }
    MS.InformationManagement.UserProfile.Notification.insertAfter(userState.preloader);
}

MS.InformationManagement.UserProfile.OnAjaxException = function (exception, userState) {
    userState.preloader.removeClass('inline');
    userState.submitButton.attr('disabled', 'disable');
    MS.InformationManagement.Common.ShowStatus('Ha ocurrido una excepción mientras se procesaba su peticion: ' + exception.get_message(), 'red', 6000);
}